package com.cxg.fairy.ec.main.index;

import android.content.ClipData;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.ui.recycler.DataConverter;
import com.cxg.fairy.ui.recycler.ItemType;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;

import java.util.ArrayList;

/**
 * Created by CFairy
 */

public final class IndexDataConverter extends DataConverter {

    @Override
    public ArrayList<MultipleItemEntity> convert() {
        final JSONArray dataArray = JSON.parseObject(getJsonData()).getJSONArray("data");
        final int size = dataArray.size();
        final ArrayList<Object> mSnapItemList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            final JSONObject data = dataArray.getJSONObject(i);

            final String imageUrl = data.getString("imageUrl");
            final String iconUrl = data.getString("iconUrl");
            final String text = data.getString("text");
            final int spanSize = data.getInteger("spanSize");
            final int id = data.getInteger("goodsId");
            final JSONArray banners = data.getJSONArray("banners");
            final String headerMainAd = data.getString("headerMainAd");

            final String mainTitle = data.getString("mainTitle");
            final String subTitle = data.getString("subTitle");
            final String indexImgA = data.getString("indexImgA");
            final String indexImgB = data.getString("indexImgB");

            final int indexImgIDA = data.getInteger("indexImgIdA")==null?0:data.getInteger("indexImgIdA");
            final int indexImgIDB = data.getInteger("indexImgIdB")==null?0:data.getInteger("indexImgIdB");

            final String goodsTag = data.getString("goodsTag");
            final String goodsName = data.getString("goodsName");
            final String goodsPrice = data.getString("goodsPrice");
            final String goodsUnit = data.getString("goodsUnit");

            final String itemType = data.getString("itemType");

            final String jumpValue = data.getString("JumpValue");

            int multiSnapStatus = 1;
            int milliseconds = 0;

            JSONArray multiSnapItemList = new JSONArray();
            try {
                milliseconds = data.getInteger("remainTime");
                multiSnapStatus = data.getInteger("multiSnapStatus");
                multiSnapItemList = data.getJSONArray("multiSnapItems");
            } catch (Exception e) {
                e.printStackTrace();
            }

            final ArrayList<String> bannerImages = new ArrayList<>();
            int type = 0;
            if("NAVIBUTTON".equals(itemType)){
                type = ItemType.ICON_IMAGE;
            } else if (imageUrl == null && text != null ) {
                type = ItemType.TEXT;
            } else if (imageUrl != null && text == null && goodsTag == null) {
                type = ItemType.IMAGE;
            } else if (imageUrl != null && goodsTag == null) {
                type = ItemType.TEXT_IMAGE;
            } else if (banners != null) {
                type = ItemType.BANNER;
                //Banner的初始化
                final int bannerSize = banners.size();
                for (int j = 0; j < bannerSize; j++) {
                    final String banner = banners.getString(j);
                    bannerImages.add(banner);
                }
            } else if (multiSnapStatus > 0 && multiSnapItemList.size() > 0) {
                type = ItemType.MULTI_SNAP_RECYCLERVIEW;
                final int multiSnapItemSize = multiSnapItemList.size();
                for (int k = 0 ; k < multiSnapItemSize;k++){
                    final JSONObject multiSnapItem = new JSONObject();
                    multiSnapItem.put("item_name", multiSnapItemList.getJSONObject(k).getString("item_name"));
                    multiSnapItem.put("item_price", multiSnapItemList.getJSONObject(k).getString("item_price"));
                    multiSnapItem.put("item_units", multiSnapItemList.getJSONObject(k).getString("item_units"));
                    multiSnapItem.put("item_image", multiSnapItemList.getJSONObject(k).getString("item_image"));
                    mSnapItemList.add(k, multiSnapItem);
                }
            } else if (headerMainAd != null) {
                type = ItemType.HEADER_MAIN_IMG ;
            } else if (goodsTag != null) {
                type = ItemType.INDEX_GOODS;
            } else if (indexImgA != null) {
                type = ItemType.INDEX_IMGS;
            } else if (mainTitle != null) {
                type = ItemType.INDEX_TITLES;
            }

            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setField(MultipleFields.ITEM_TYPE, type)
                    .setField(MultipleFields.ITEMTYPE, itemType)
                    .setField(MultipleFields.SPAN_SIZE, spanSize)
                    .setField(MultipleFields.ID, id)
                    .setField(MultipleFields.TEXT, text)
                    .setField(MultipleFields.IMAGE_URL, imageUrl)
                    .setField(MultipleFields.ICON_URL, iconUrl)
                    .setField(MultipleFields.BANNERS, bannerImages)
                    .setField(MultipleFields.SNAP_ITEMS_REMAIN_TIME,milliseconds)
                    .setField(MultipleFields.SNAP_ITEMS, mSnapItemList)
                    .setField(MultipleFields.HEADER_MAIN_IMG_URL, headerMainAd)
                    .setField(MultipleFields.GOODS_TAG, goodsTag)
                    .setField(MultipleFields.GOODS_NAME, goodsName)
                    .setField(MultipleFields.GOODS_PRICE, goodsPrice)
                    .setField(MultipleFields.GOODS_UNIT, goodsUnit)
                    .setField(MultipleFields.INDEX_IMG_A, indexImgA)
                    .setField(MultipleFields.INDEX_IMG_B, indexImgB)
                    .setField(MultipleFields.INDEX_IMG_A_ID, indexImgIDA)
                    .setField(MultipleFields.INDEX_IMG_B_ID, indexImgIDB)
                    .setField(MultipleFields.MAIN_TITLE, mainTitle)
                    .setField(MultipleFields.SUB_TITLE, subTitle)
                    .setField(MultipleFields.JUMP_VALUE,jumpValue)
                    .build();

            ENTITIES.add(entity);

        }

        return ENTITIES;
    }

    @Override
    public MultipleItemEntity convertHeader() {
        return null;
    }
}
